package dbexp.model;

import java.util.ArrayList;

import dbexp.model.db.DbOT;

public class View extends SourceType {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public static final String LABEL = "widoki";
	public static final String DEF_ICON = "view-icon";
	private boolean force = false;

	public View() {
		super(LABEL, DbOT.VIEWS);
		setIcons(DEF_ICON);
	}

	public View(String name) {
		super(name, DbOT.VIEW);
		setIcons(DEF_ICON);
		set("leaf", true);
	}

	public View(String name, String sch) {
		this(name);
		setSchema(sch);
	}

	public void setSchema(String schema) {
		set("schema", schema);
	}

	public String getSchema() {
		return get("schema");
	}

	public boolean isForce() {
		return this.force;
	}

	public void setForce(boolean force) {
		this.force = force;
	}

	public void setContent(String content) {
		set("text", content);
	}

	public String getContent() {
		return get("text");
	}

	public ArrayList<Error> getErrors() {
		return super.getErrors();
	}

	public String getErrorString() {
		String out = " ";
		for (int i = 0; i < getBledy().size(); i++) {
			out += getErrors().get(i).getText();
		}
		return out;
	}

	public void setAliases(String aliases) {
		set("aliases", aliases);
	}

	public String getAliases() {
		return get("aliases");
	}

	private String getQuery(String oper) {
		if (requiredFieldIsNotNull()) {
			String query = "VIEW " + getObjectNameForQuery();

			return oper + query + " AS " + getContent();
		} else {
			return oneFromDual();
		}
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE OR REPLACE " + (isForce() ? "FORCE " : ""));
		} else if (operation == ALTER) {
			return getQuery("ALTER");
		} else if (operation == DELETE) {
			return "DROP VIEW " + getObjectNameForQuery() + "";
		}
		return oneFromDual();
	}
}
